import React, { useContext } from 'react';
import FormItem from './EditFormItem';
import EditTableRowContext from './context/EditTableRowContext';
import type { EditTableCellProps } from './interface';

const EditTableCell: React.FC<EditTableCellProps> = (props) => {
  const {
    rowIndex,
    record,
    formItemProps,
    valueType,
    editable,
    children,
    ...restProps
  } = props;
  const { form } = useContext(EditTableRowContext);

  // console.log('--EditTableCell--record--rowIndex--:', record, rowIndex);

  let childNode: React.ReactNode;

  if (editable) {
    childNode = (
      <FormItem
        valueType={valueType}
        record={record}
        form={form!}
        formItemProps={formItemProps}
      />
    );
  } else {
    childNode = children;
  }

  return <td {...restProps}>{childNode}</td>;
};

export default EditTableCell;
